<?php

namespace app\admin\controller\zhongzhuan\fenpei;

use app\common\controller\Backend;
use think\Db;
use fast\Tree;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Assignkehu extends Backend
{

    /**
     * Assignkehu模型对象
     * @var \app\admin\model\zhongzhuan\fenpei\Assignkehu
     */
    protected $model = null;
    
    protected $multiFields = 'auto_assign';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\zhongzhuan\fenpei\Assignkehu;
        
        $this->assigndetailmodel = new \app\admin\model\zhongzhuan\fenpei\Assignkehudetail;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    
    
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $ids = $this->request->param('ids',0);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
//            $request_data = $this->request->params();
//            var_dump($request_data);exit;
            
            $list = db('ec_user')->alias('u')
                    ->field('b.people,b.success_people,b.assign_status,u.name,u.mobile,u.id,u.is_shangban,u.deptId')
                    ->join('ec_assign_kehu_detail b','u.id = b.user_id','left')
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit)->each(function($item){
                        $deparment_name = '';
                        if(!empty($item['deptId'])){
                            $deparment_name = db('ec_department')->where('deptId',$item['deptId'])->value('deptName');
                        }
                        
                        $item['deptName'] = $deparment_name;
                        return $item;
                    });

            
                
     


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }   
        $depeartment_list = db('ec_department')->field('deptId id,parentDeptId pid,deptName name,"true" spread')->select();
        $treeobj = Tree::instance();
        $treeobj->init($depeartment_list);
        $result = $treeobj->getTreeArray(0);
//        $departmentlist =[
//            [
//                'title'=>'A-1','id'=>1,'field'=>'name1','checked'=>true,'spread'=>true,
//                'children'=>[
//                     ['title'=>'A-3dfdsfsd','id'=>1,'field'=>'name1','checked'=>true,'spread'=>true,]
//                ]
//            ]
//        ];

        $this->assignconfig('departmentlist', $result);
        return $this->view->fetch();
    }
    
    
    public function edit($ids = null)
    {
        
        
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        
        $result = false;
        Db::startTrans();
        try {
            if(!empty($params['id'])){
                $exist_info = db('ec_assign_kehu_detail')->where('user_id',$params['id'])->count();
                if(!empty($exist_info)){
                    $result = db('ec_assign_kehu_detail')->where('user_id',$params['id'])->update(['people'=>$params['people'],'updatetime'=>time()]);
                }else{
                    $result = db('ec_assign_kehu_detail')->insertGetId(['assign_id'=>0,'people'=>$params['people'],'user_id'=>$params['id'],'createtime'=>time()]);
                }
                
                
            }
            //是否采用模型验证
            
//            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }
    
    
    
    /**
     * 批量更新
     *
     * @param $ids
     * @return void
     */
    public function multi($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $ids = $ids ?: $this->request->post('ids');
        if (empty($ids)) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }

        if (false === $this->request->has('params')) {
            $this->error(__('No rows were updated'));
        }
        parse_str($this->request->post('params'), $values);
        $result = false;
        $count = 0;
        Db::startTrans();
        try {
            $result = db('ec_user')->where('id',$ids)->update($values);
            
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        
        $this->success();
        
    }

    
  


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
